package com.aiyan.edu.nacos.consumer.example;

import com.aiyan.edu.nacos.consumer.example.client.HelloClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.*;

/**
 * @author: zqzhou
 * @create: 2019/12/11 14:28
 **/
@EnableFeignClients
@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
@RestController
public class NacosConsumerApplication {

    public static String[] args;
    public static ConfigurableApplicationContext applicationContext;

    public static void main(String[] args) {
        NacosConsumerApplication.args = args;
        NacosConsumerApplication.applicationContext = SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }


//        @Autowired
//        LoadBalancerClient loadBalancerClient;
//
//        @Autowired
//        RestTemplate restTemplate;

//    @Value("${aiyan.nacos.hello}")
//    String helloConfig;
//
//    @Value("${redis.host}")
//    String redisHost;
//    @Value("${redis.port}")
//    String redisPort;
//    @Value("${redis.password}")
//    String redisPassword;
//    @Value("${redis.database}")
//    String redisDatabase;
////
//    @Autowired
//    HelloClient helloClient;
////
//    @GetMapping("/consumer/sayhello/{id}")
//    public String sayhello(@PathVariable("id") String id) {
////            ServiceInstance serviceInstance = this.loadBalancerClient.choose("aiyanedu-nacos-provider-example");
////
////            String url = serviceInstance.getUri() + "/provider/hello/" + id;
////
////            String object = this.restTemplate.getForObject(url, String.class);
//
//        String hello = this.helloClient.hello(id);
//        return this.helloConfig + " return : " + hello;
//    }
//
//
//    @GetMapping("/consumer/redis")
//    public String redis() {
//        return this.redisHost + ":" + this.redisPort + " set auth " + this.redisPassword + ", database use " + this.redisDatabase;
//    }
//
    @GetMapping("/reboot")
    public String reboot() {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                NacosConsumerApplication.applicationContext.close();
                NacosConsumerApplication.applicationContext = SpringApplication.run(NacosConsumerApplication.class, NacosConsumerApplication.args);
            }
        });
        executorService.shutdown();

        return "reboot success.";
    }
}
